package com.sinocare.project.maintenance.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sinocare.project.common.entity.SccRole;
import com.sinocare.project.common.util.JwtTokenUtil;
import com.sinocare.project.maintenance.realm.MyRealm;
import com.sinocare.project.maintenance.service.SncUserLoginService;
import com.sinocare.project.common.entity.SncUserLogin;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户登录表(SncUserLogin)表控制层
 *
 * @author makejava
 * @since 2021-12-21 15:54:53
 */
@RestController
@RequestMapping("sncUserLogin")
@CrossOrigin
public class SncUserLoginController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SncUserLoginService sncUserLoginService;

    /**
     * 分页查询所有数据
     *
     * @param count     账号
     * @param password 密码
     * @return 所有数据
     */
    @GetMapping(value = "login")
    public Result login(String count, String password) {
        DefaultSecurityManager manager = new DefaultSecurityManager();
        manager.setRealm(new MyRealm());
        SecurityUtils.setSecurityManager(manager);
        Subject subject = SecurityUtils.getSubject();
        QueryWrapper<SncUserLogin> wrapper = new QueryWrapper<>();
        wrapper.eq("count", count);
        SncUserLogin one = this.sncUserLoginService.getOne(wrapper);
        UsernamePasswordToken token = new UsernamePasswordToken(count, password);
        HashMap<String, Object> map = new HashMap<>();
        try {
            subject.login(token);
            System.out.println("登录成功");
            subject.hasRole("");
            QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("c.id",one.getId());
            SccRole byUserId = this.sncUserLoginService.getByUserId(queryWrapper);
            map.put("userId",one.getId());
            map.put("roleName",byUserId.getRoleName());
            String token1 = JwtTokenUtil.getToken(map);
            return Result.ok(ResultCode.SUCCESS,token1);
        } catch (UnknownAccountException e) {
            System.out.println("没有此用户");
            e.printStackTrace();
            token.clear();
            return Result.fail(ResultCode.FAIL,"没有此用户");
        } catch (IncorrectCredentialsException e) {
            System.out.println("密码错误");
            e.printStackTrace();
            token.clear();
            return Result.fail(ResultCode.FAIL,"密码错误");
        }catch (AuthenticationException e) {
            System.out.println("账号已被冻结");
            e.printStackTrace();
            token.clear();
            return Result.fail(ResultCode.FAIL,"账号已被冻结");
        }catch (Exception e) {
            System.out.println("exception");
            e.printStackTrace();
            token.clear();
            return Result.fail(ResultCode.FAIL,"密码错误");
        }
    }

    /**
     * shiro登出方法
     * @return
     */
    @GetMapping(value = "logout")
    public Result logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        System.out.println("请求了登出方法");
        return Result.ok(ResultCode.SUCCESS, "登出");
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return Result.ok(ResultCode.SUCCESS, this.sncUserLoginService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sncUserLogin 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody SncUserLogin sncUserLogin) {
        return Result.ok(ResultCode.SUCCESS, this.sncUserLoginService.save(sncUserLogin));
    }

    /**
     * 修改数据
     *
     * @param sncUserLogin 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result update(@RequestBody SncUserLogin sncUserLogin) {
        return Result.ok(ResultCode.SUCCESS, this.sncUserLoginService.updateById(sncUserLogin));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestParam("idList") List<Long> idList) {
        return Result.ok(ResultCode.SUCCESS, this.sncUserLoginService.removeByIds(idList));
    }
}

